<%@ page import="java.lang.*" pageEncoding="utf-8"%>
<%@ page import="java.util.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="java.io.*"%>
 
<%
	request.setCharacterEncoding("utf-8");
	response.setCharacterEncoding("utf-8");
	ServletContext context = getServletContext();
	final long MAX_SIZE = 2 * 1024 * 1024;
	final String[] allowedExt = { "jpg", "jpeg", "gif", "bmp", "png" };
	DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
	diskFileItemFactory.setSizeThreshold(4096);
	String realPath = context.getRealPath("/") + "kindeditor-3.2.1/attached";
	diskFileItemFactory.setRepository(new File(realPath));
	ServletFileUpload servletFileUpload = new ServletFileUpload(
			diskFileItemFactory);
	servletFileUpload.setFileSizeMax(MAX_SIZE);
 
	List<FileItem> fileList = null;
	try {
		fileList = servletFileUpload.parseRequest(request);
	} catch (FileUploadException fue) {
		fue.printStackTrace();
		out.println(alert("上传文件失败"));
		return;
	}
	
	FileItem fileItem = null;
	String path = null;
	long size = 0;
	fileItem = (FileItem) fileList.get(2);
	path = fileItem.getName();
	size = fileItem.getSize();
	if (null == path || "".equals(path) || size == 0) {
		out.println(alert("请输入您要上传的文件"));
		return;
	}
 
	String separator = File.separator;
	String t_name = path.substring(path.lastIndexOf(separator) + 1);
	String t_ext = t_name.substring(t_name.lastIndexOf(".") + 1);
	boolean errExt = true;
	for (int i = 0; i < allowedExt.length; i++) {
		if (allowedExt[i].equals(t_ext)) {
			errExt = false;
			break;
		}
	}
	if (errExt) {
		out.println(alert("请选择正确格式的文件上传"));
		return;
	}
 
	long now = System.currentTimeMillis();
	String prefix = String.valueOf(now);
	String filename = prefix + "." + t_ext;
	String u_name = context.getRealPath("/") + "kindeditor-3.2.1/attached/"
			+ filename;
 
	try {
		fileItem.write(new File(u_name));
	} catch (Exception e) {
		e.printStackTrace();
		out.println(alert("上传文件失败"));
	}
 
	String id = fileList.get(0).getString();
	String imgTitle = fileList.get(4).getString();
	String imgWidth = fileList.get(5).getString();
	String imgHeight = fileList.get(6).getString();
	String imgBorder = fileList.get(7).getString();
	out.println("<html>");
	out.println("<head>");
	out.println("<title>Insert Image</title>");
	out
			.println("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" \\>");
	out.println("</head>");
	out.println("<body>");
	out.println("<script type=\"text/javascript\">parent.KE.plugin[\"image\"].insert(\""
					+ id
					+ "\", \""
					+ request.getContextPath() + "/kindeditor-3.2.1/attached/" + filename
					+ "\", \""
					+ imgTitle
					+ "\", \""
					+ imgWidth
					+ "\", \""
					+ imgHeight
					+ "\", \""
					+ imgBorder
					+ "\");</script>");
	out.println("</body>");
	out.println("</html>");
%>
 
 
<%!public String alert(String msg) {
		StringBuilder str = new StringBuilder();
 
		str.append("<html>");
		str.append("<head>");
		str.append("<title>error</title>");
		str
				.append("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">");
		str.append("</head>");
		str.append("<body>");
		str.append("<script type=\"text/javascript\">alert(\"" + msg
				+ "\");history.back();</script>");
		str.append("</body>");
		str.append("</html>");
 
		return str.toString();
}%>